import { createRouter, createWebHistory } from 'vue-router';


import Home from "../views/Home/index.vue"
import Login from "../views/Login/index.vue"
import Self from "../views/self/index.vue"



const routes = [
    {
        path: '/',
        redirect: '/home'
    },   
    {
        path:'/home',
        name:'home',
        component:Home,
        meta:{ is_footer:true ,is_header:true}
    },
    {
        path:'/login',
        name:'login',
        component:Login,
        meta:{ is_footer:false,is_header:true}
    },
    {
        path:'/self',
        name:'self',
        component:Self,
        meta:{ is_footer:true }
    },
    {
        path:'/cate',
        name:'cate',
        component: () => import('../views/cate/index.vue'),
        meta:{is_footer:true ,is_header:true }
    },
    {
        path:'/cart',
        name:'cart',
        component: () => import('../views/cart/index.vue'),
        meta:{is_footer:true ,is_header:true }
    },
    {
        path:'/list',
        name:'list',
        component: () => import('../views/list/index.vue'),
        meta:{is_header:true }
    },
    {
        path:'/detail',
        name:'detail',
        component: () => import('../views/detail/index.vue'),
        meta:{is_header:true }
    },
    {
        path:'/search',
        name:'search',
        component: () => import('../views/search/index.vue'),
        meta:{is_header:true }
    },
    {
        path:'/register',
        name:'register',
        component:()=> import('../views/register/index.vue'),
        meta:{is_header:true}

    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 准备一个数组, 表示需要验证登录的路由
const list: Array<string> = ['/self']

// 配置全局守卫来进行拦截操作
router.beforeEach(to => {
  // 判断去到的目的地如果在 list 数组内出现, 表示需要验证
  if (list.indexOf(to.fullPath) === -1) return true

  // 表示你要去到的目的地包含在 list 数组内
  // 从 localStorage 内拿到 token
  const token: string = window.localStorage.getItem('token') || ''
  if (token) return true
    console.log(123);
    

  // 表示 localStorage 内没有 token, 那么肯定是没有登录过
  // 直接跳转到 /login 路由
  router.push('/login')

  return false
})

export default router